﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Movies.Web.Models;
using Movies.Web.Models.ViewModel;

namespace Movies.Web.Controllers
{
    public class MoviesController : Controller
    {
        public readonly static ICollection<Movie> _moviesDb = MvcApplication.Movies;
        public readonly static ICollection<Actor> _actorDb = MvcApplication.Actors;
        


        //
        // GET: /Movies/
        public ActionResult Index()
        {
            return View(_moviesDb);
        }

        //
        // GET: /Movies/Details/5

        public ActionResult Details(int id)
        {
            var movie = _moviesDb.Where(x => x.Id == id).SingleOrDefault();

            if (movie == null)
            {
                TempData.Add("errorMessage", "The Id was not found, please choose from the following list");
                return RedirectToAction("index");
            }

            return View(movie);
        }

        //
        // GET: /Movies/Create

        public ActionResult Create()
        {
            ViewBag.ActorChoices = _actorDb.ToDictionary(x => x.Id, y => y.FullName);

            return View();
        } 

        //
        // POST: /Movies/Create

        [HttpPost]
        public ActionResult Create(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.ActorChoices = _actorDb.ToDictionary(x => x.Id, y => y.FullName);
                return View();
            }

            try
            {
                    // TODO: Add insert logic here
                    movie.Id = _moviesDb.Count + 1;
                    _moviesDb.Add(movie);
              

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                TempData.Add("errorMessage", ex.Message);
                return View();
            }
        }
        
        //
        // GET: /Movies/Edit/5
 
        public ActionResult Edit(int id)
        {
            var movie = _moviesDb.Where(x => x.Id == id).SingleOrDefault();

            if (!HasValidMovie(movie))
            {
                return RedirectToAction("index");
            }

            return View(movie);
        }

        //
        // POST: /Movies/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Movie movieToUpdate)
        {
            if (!ModelState.IsValid) return View();

            try
            {
                var movie = _moviesDb.Where(x => x.Id==id).SingleOrDefault();

                if (!HasValidMovie(movie))
                {
                    return RedirectToAction("index");
                }

                
                    // remap
                    movie.Name = movieToUpdate.Name;
                    movie.Rating = movieToUpdate.Rating;
                    movie.RelaseDate = movieToUpdate.RelaseDate;
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Movies/Delete/5
        public ActionResult Delete(int id)
        {
            var movie = _moviesDb.Where(x => x.Id == id).SingleOrDefault();

            if (!HasValidMovie(movie))
            {
                return RedirectToAction("index");
            }


            return View(movie);
        }

        private bool HasValidMovie(Movie movie)
        {
            bool isValid = false;

            if (movie == null)
            {
                TempData.Add("errorMessage", "The Id was not found, please choose from the following list");
            }
            else
            {
                isValid = true;
            }

            return isValid;
        }

        //
        // POST: /Movies/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Movie movieToDelete)
        {
            try
            {
                // TODO: Add delete logic here
                var movie = _moviesDb.Where(x => x.Id == id).SingleOrDefault();

                if (!HasValidMovie(movie))
                {
                    return RedirectToAction("index");
                }

                _moviesDb.Remove(movie);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
